//package com.rc.evcharger.rabbit;
//
//
//import com.alibaba.fastjson.JSON;
//import com.google.gson.Gson;
//import com.rc.evcharger.common.config.MqCommandConfig;
//import com.rc.evcharger.common.config.RabbitListenerConstant;
//import com.rc.evcharger.model.order.OrderInfo;
//import com.rc.evcharger.redis.RedisService;
//import com.rc.evcharger.service.log.ChargedProcessLogService;
//import com.rc.evcharger.service.order.OrderInfoService;
//import com.rc.evcharger.vo.charge.StartChargeReq;
//import com.rc.evcharger.vo.charge.StopChargeReq;
//import com.rc.evcharger.vo.log.ChargedProcessLog;
//import com.rc.evcharger.action.StdBizAction;
//import com.rc.evcharger.client.ChargerClientRouter;
//import com.rc.evcharger.dto.msg.TerminalMessage;
//import com.rc.evcharger.mq.AsyncTask;
//import com.rabbitmq.client.Channel;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
///**
// * 启停充电（平台）指令监听器
// * Created by sven on 2019/3/4
// */
//@Component
//public class Domain1Receiver implements ChannelAwareMessageListener {
//    private static Logger logger = LogManager.getLogger(Domain1Receiver.class);
//
//    @Autowired
//    private StdBizAction stdBizAction;
//
//    @Autowired
//    private AsyncTask asyncTask;
//
//    @Autowired
//    private MqCommandConfig mqCommandConfig;
//
//    @Autowired
//    private RedisService redisService;
//
//    @Autowired
//    private ChargerClientRouter chargerClientRouter;
//
//    @Autowired
//    private ChargedProcessLogService chargedProcessLogService;
//
//    @Autowired
//    private OrderInfoService orderInfoService;
//
//    //@RabbitListener(queues = "hello")
//    //@RabbitHandler
//    //public void processHello(String msg) {
//    //    System.out.println("helloReceiver  : " + msg);
//    //}
//
//    //@RabbitListener(queues = "user")
//    //@RabbitHandler
//    //public void processUser(String msg) {
//    //    System.out.println("userReceiver  : " + msg);
//    //}
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_A1_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_A1_QUEUE})
//    @RabbitHandler
//    public void processA1(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("commandA1Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.login(terminalMessage);
//        } catch (Exception e) {
//            logger.error("commandA1Listener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_A2_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_A2_QUEUE})
//    @RabbitHandler
//    public void processA2(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("commandA2Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.logout(terminalMessage);
//        } catch (Exception e) {
//            logger.warn("commandA2Listener 处理失败", e);
//            return;
//        }
//    }
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_A4_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_A4_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_A4_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_A4_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_A4_QUEUE}, containerFactory = "customContainerFactory")
//    @RabbitHandler
//    public void processA4(String msg, Channel channel) {
//        //logger.info("commandA4Listener  1收到了:{}", msg);
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            //logger.info("commandA4Listener 2桩编号:{}, 时间差:{}毫秒,terminalMessage:{}", terminalMessage.getRtua(), System.currentTimeMillis() - terminalMessage.getCurrentTimeMillis(), terminalMessage.getBody());
//            //stdBizAction.hearBeat(terminalMessage);
//            asyncTask.hearBeat(terminalMessage);
//            //stdBizAction.hearBeat(terminalMessage);
//        } catch (Exception e) {
//            logger.warn("commandA4Listener 处理失败", e);
//            return;
//        }
//    }
//
//    //手动确认
//    //@RabbitListener(queues = RabbitListenerConstant.DOMAIN1_CONTROLL_A4_QUEUE, containerFactory = "rabbitListenerContainerFactoryManual")
//    //@RabbitHandler
//    //public void processA4(Message message, Channel channel) {
//    //    try {
//    //        String msg = new String(message.getBody(), "UTF-8");
//    //        //logger.info("commandA4Listener  1收到了:{}", msg);
//    //        Map<String, Object> headers = message.getMessageProperties().getHeaders();
//    //        String headerApplicationName = "";
//    //        Long currentTimeMillis = null;
//    //
//    //        //获取消息头数据
//    //        if (headers.size() > 0) {
//    //            headerApplicationName = (String) headers.get(RabbitListenerConstant.HEADER_APPLICATION_NAME);
//    //            currentTimeMillis = (Long) headers.get(RabbitListenerConstant.HEADER_CURENT_TIMEMILLIS);
//    //
//    //            int i = RabbitUtil.subTimestamp(System.currentTimeMillis(), currentTimeMillis);
//    //            //防止心跳积压宕机。15秒释放
//    //            if (i > RabbitListenerConstant.HEADER_MESSAGE_TIME_OUT * 1000) {
//    //                logger.error("processA4心跳超时，对应msg:{}", msg);
//    //                return;
//    //            }
//    //        }
//    //
//    //        //心跳消息的IP不符时，重新抛回队列
//    //        if (!mqCommandConfig.getApplicationName().equals(headerApplicationName)) {
//    //            long tag = message.getMessageProperties().getDeliveryTag();
//    //            channel.basicReject(tag, false);
//    //            return;
//    //        }
//    //
//    //        //消息处理
//    //        TerminalMessage terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//    //        //logger.info("commandA4Listener 2桩编号:{}, 时间差:{}毫秒,terminalMessage:{}", terminalMessage.getRtua(), System.currentTimeMillis() - terminalMessage.getCurrentTimeMillis(), terminalMessage.getBody());
//    //        //stdBizAction.hearBeat(terminalMessage);
//    //        asyncTask.hearBeat(terminalMessage);
//    //
//    //        // 采用手动应答模式, 手动确认应答更为安全稳定
//    //        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
//    //
//    //    } catch (Exception e) {
//    //        logger.error("commandA4Listener 处理失败", e);
//    //        return;
//    //    }
//    //}
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_A5_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_A5_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_A5_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_A5_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_A5_QUEUE}, containerFactory = "customContainerFactory")
//    @RabbitHandler
//    public void processA5(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("commandA5Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            asyncTask.chargeReport(terminalMessage);
//            //stdBizAction.chargeReport(terminalMessage);
//        } catch (Exception e) {
//            logger.error("commandA5Listener 处理失败", e);
//            return;
//        }
//    }
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_A6_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_A6_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_A6_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_A6_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_A6_QUEUE})
//    @RabbitHandler
//    public void processA6(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("commandA6Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//
//            stdBizAction.chargeBalance(terminalMessage);
//        } catch (Exception e) {
//            logger.error("commandA6Listener 处理失败", e);
//            return;
//        }
//    }
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_CARDNO_QUEUE})
//    @RabbitHandler
//    public void processCardNo(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("processCardNo 收到了:{},channel:{}", msg);
//
//        StartChargeReq startChargeRequest;
//        try {
//            startChargeRequest = JSON.parseObject(msg, StartChargeReq.class);
//            startChargeRequest.setStartType("1");//启动方式 0 一般 1 刷卡 2 VIN码启动 默认0
//            //commandService.query_start_charge(startChargeRequest);
//            //远程调用刷卡启动指令
//            chargerClientRouter.query_start_charge(startChargeRequest);
//        } catch (Exception e) {
//            logger.error("processCardNo 处理失败", e);
//            return;
//        }
//    }
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_1C_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_1C_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_1C_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_1C_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_1C_QUEUE,})
//    @RabbitHandler
//    public void process1C(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command1CListener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.readCardNo(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command1CListener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_8C_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_8C_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_8C_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_8C_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_8C_QUEUE})
//    @RabbitHandler
//    public void process8C(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command8CListener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.readUnLock(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command8CListener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_8E_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_8E_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_8E_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_8E_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_8E_QUEUE})
//    @RabbitHandler
//    public void process8E(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command8EListener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//
//            //启动和停止是同一指令，需要在MQ监听器进行区别
//            stdBizAction.readElecControl(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command8EListener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_8E_DELAY_STOP_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_8E_DELAY_STOP_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_8E_DELAY_STOP_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_8E_DELAY_STOP_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_8E_DELAY_STOP_QUEUE})
//    @RabbitHandler
//    public void process8EDelayStop(String msg) {
//        logger.info("command8EDelayStopListener 收到了:{},channel:{}", msg);
//        try {
//            StopChargeReq stopChargeReq = JSON.parseObject(msg, StopChargeReq.class);
//            //查询订单是否已结算
//            OrderInfo order = orderInfoService.selectByOrderNum(stopChargeReq.getOrderNum());
//            if ("2".equals(order.getStatus())) {
//                return;
//            }
//            //commandService.query_stop_charge(stopChargeReq);
//            //远程调用停止指令
//            chargerClientRouter.query_stop_charge(stopChargeReq);
//        } catch (Exception e) {
//            logger.error("command8EDelayStopListener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_33_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_33_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_33_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_33_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_33_QUEUE})
//    @RabbitHandler
//    public void process33(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command33Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.powerOff(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command33Listener 处理失败", e);
//            return;
//        }
//    }
//
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_79_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_79_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_79_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_79_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_79_QUEUE,})
//    @RabbitHandler
//    public void process79(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command79Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.readVinControl(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command79Listener 处理失败", e);
//            return;
//        }
//    }
//
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_CONTROLL_89_QUEUE,
//            RabbitListenerConstant.DOMAIN2_CONTROLL_89_QUEUE,
//            RabbitListenerConstant.DOMAIN3_CONTROLL_89_QUEUE,
//            RabbitListenerConstant.DOMAIN4_CONTROLL_89_QUEUE,
//            RabbitListenerConstant.DOMAIN5_CONTROLL_89_QUEUE})
//    @RabbitHandler
//    public void process89(String msg) {
//        //String msg = new String(message.getBody(), "UTF-8");
//        logger.info("command89Listener 收到了:{},channel:{}", msg);
//
//        TerminalMessage terminalMessage;
//        try {
//            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);
//            stdBizAction.elecPileFault(terminalMessage);
//        } catch (Exception e) {
//            logger.error("command89Listener 处理失败", e);
//            return;
//        }
//    }
//
//    /**
//     * todo 充电日志
//     *
//     * @param msg
//     */
//    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN_LOG_CHARGED_PROCESS_QUEUE,
//            RabbitListenerConstant.DOMAIN1_LOG_CHARGED_PROCESS_QUEUE,
//            RabbitListenerConstant.DOMAIN2_LOG_CHARGED_PROCESS_QUEUE})
//    @RabbitHandler
//    public void processChargeLog(String msg) {
//        logger.info("processChargeLog 收到了:{},channel:{}", msg);
//        try {
//            ChargedProcessLog log = new Gson().fromJson(msg, ChargedProcessLog.class);
//            //新增的MONGODB LOG
//            chargedProcessLogService.addChargedProcessLog(log);
//        } catch (Exception e) {
//            logger.error("processChargeLog 处理失败", e);
//            return;
//        }
//    }
//
//    @Override
//    public void onMessage(Message message, Channel channel) throws Exception {
//
//    }
//}
